package com.ruoyi.web.controller.mall;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.MallCategory;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.mall.domain.MallGoods;
import com.ruoyi.mall.service.MallCategoryService;
import com.ruoyi.mall.service.MallGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 商品Controller
 *
 * @author ruoyi
 * @date 2024-01-11
 */
@Api(tags = "商品管理")
@RestController
@RequestMapping("/mall/goods")
public class MallGoodsController extends BaseController {
    @Autowired
    private MallGoodsService mallGoodsService;
    @Autowired
    private MallCategoryService mallCategoryService;

    /**
     * 查询商品列表
     */
    @ApiOperation("查询商品列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:list')")
    @PostMapping("/list")
    public TableDataInfo list(MallGoods mallGoods) {
        startPage();
        List<MallGoods> list = mallGoodsService.selectMallGoodsList(mallGoods);
        // 查询所有分类
        List<MallCategory> categories = mallCategoryService.selectMallCategoryList(null);
        Map<Long, MallCategory>  categoryMap = categories.stream().collect(Collectors.toMap(MallCategory::getCateId, Function.identity(), (o, n) -> n));
        // 设置分类名称
        for (MallGoods goods : list) {
            setCateName(goods, goods.getCateId(), categoryMap);
        }
        return getDataTable(list);
    }

    private void setCateName(MallGoods goods, Long cateId, Map<Long, MallCategory>  categoryMap) {
        if (cateId == null || cateId.equals(0L)) {
            return;
        }
        MallCategory category = categoryMap.get(cateId);
        if (category != null) {
            if (StringUtils.isNotBlank(goods.getCateName())) {
                goods.setCateName(category.getCateName() + "->" + goods.getCateName());
            } else {
                goods.setCateName(category.getCateName());
            }
            setCateName(goods, category.getParentId(), categoryMap);
        }
    }

    /**
     * 导出商品列表
     */
    @ApiOperation("导出商品列表")
    @PreAuthorize("@ss.hasPermi('mall:goods:export')")
    @Log(title = "商品", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MallGoods mallGoods) {
        List<MallGoods> list = mallGoodsService.selectMallGoodsList(mallGoods);
        ExcelUtil<MallGoods> util = new ExcelUtil<>(MallGoods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 获取商品详细信息
     */
    @ApiOperation("获取商品详细信息")
    @PreAuthorize("@ss.hasPermi('mall:goods:query')")
    @PostMapping(value = "getInfo/{goodsId}")
    public AjaxResult getInfo(@PathVariable("goodsId") Long goodsId) {
        return success(mallGoodsService.selectMallGoodsByGoodsId(goodsId));
    }

    /**
     * 保存商品
     */
    @ApiOperation("保存商品")
    @PreAuthorize("@ss.hasPermi('mall:goods:add')")
    @Log(title = "商品", businessType = BusinessType.SAVE)
    @PostMapping("save")
    public AjaxResult save(@RequestBody MallGoods mallGoods) {
        if (mallGoods.getGoodsId() == null) {
            return toAjax(mallGoodsService.insertMallGoods(mallGoods));
        } else {
            return toAjax(mallGoodsService.updateMallGoods(mallGoods));
        }
    }

    /**
     * 删除商品
     */
    @ApiOperation("删除商品")
    @PreAuthorize("@ss.hasPermi('mall:goods:remove')")
    @Log(title = "商品", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{goodsIds}")
    public AjaxResult remove(@PathVariable Long[] goodsIds) {
        return toAjax(mallGoodsService.deleteMallGoodsByGoodsIds(goodsIds));
    }
}
